package com.mohe.shanpao.netty;

import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

public class MessageDecoder extends ByteToMessageDecoder {

	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
		if (in.readableBytes() < (8 + 4)) {
			return;
		}
		long timestamp = in.readLong();
		int length = in.readInt();
		byte[] data = new byte[length];
		in.readBytes(data);
		String value = new String(data);
		
		Message message = new Message(value,timestamp);
		
		out.add(message);
	}

}
